package com.ats.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ats.model.ClasseRecord;
import com.ats.model.Student;
import com.ats.model.User;
import com.ats.service.ClasseService_xh;
import com.ats.service.JobService_xh;
import com.ats.service.StudentService_xh;
import com.ats.service.TeacherService_xh;
import com.ats.serviceimpl.ClasseServiceImpl_xh;
import com.ats.serviceimpl.JobServiceImpl_xh;
import com.ats.serviceimpl.StudentServiceImpl_xh;
import com.ats.serviceimpl.TeacherServiceImpl_xh;
/**
 * 布置作业
 * @author xh
 *
 */
public class AddHomeJobController_xh extends HttpServlet{
	JobService_xh js =new JobServiceImpl_xh();
	ClasseService_xh clser = new ClasseServiceImpl_xh();
	TeacherService_xh tes =new TeacherServiceImpl_xh();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.setCharacterEncoding("utf8");
		HttpSession session =req.getSession();
		PrintWriter  pw = resp.getWriter();
		String jname = req.getParameter("jname");
		int cid=Integer.parseInt(req.getParameter("cid"));
		int uid =Integer.parseInt(req.getParameter("uid"));
		//新增作业
		boolean flag =js.insertJob(jname);
		if(flag){
			//获取当前时间
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date now = new Date();
			String myDate=df.format(now);
			//查询老师ID，根据用户ID
			int tid =tes.findTidByUid(uid);
			int jsid =1;
			//根据新增的作业，查询作业的ID
			int jid =js.findJidByJname(jname);
			//获取全部学生ID
			List<ClasseRecord> sids =clser.findSidByCid(cid);
			for (ClasseRecord classeRecord : sids) {
				int sid =classeRecord.getSid();
				js.insertJobRecord(myDate, jid, jsid, sid, tid);
			}
			
		}	
			pw.write(flag+"");
			pw.flush();
			pw.close();			
	}
}
